package com.easymin.weixin.controller.manager;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.easymin.weixin.entity.WxAccount;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxAccountManager;
import com.easymin.weixin.manager.WxApplicationManager;

/**
 * 客戶經理控制器
 * @author fzh
 *
 */
@Controller
@RequestMapping("wxmanager/")
public class ManagerIndexController {
	
	@Autowired
	private WxApplicationManager wxApplicationManager;
	
	@Autowired
	private WxAccountManager wxAccountManager;

	
	/**
	 * 跳转到主页
	 * @param appKey 系统Key
	 * @param appId 微信AppId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="{appKey}", method={RequestMethod.GET})
	public ModelAndView index (@PathVariable("appKey") String appKey, String appId) throws Exception {
		
		// 验证参数
		if (null == appKey || null == appId) {
			throw new Exception("参数错误");
		}

		// 查询应用
		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		if (null == wxApplication) {
			throw new Exception("没有该应用信息");
		}

		// 获取该应用账号
		WxAccount wxAccount = wxAccountManager.findBy(appId, wxApplication.getId());
		
		// 返回首页数据
		ModelAndView mv = new ModelAndView();
		mv.addObject("appKey", appKey);
		mv.addObject("wxApplication", wxApplication);
		mv.addObject("wxAccount", wxAccount);
		mv.setViewName("manager/index");
		return mv;
	}
}
